package com.example.compose

import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Build

class Constance {

    @Retention(AnnotationRetention.SOURCE)
    annotation class AppInformation{
        companion object{
            private val packageManager: PackageManager
            private val packageInfo: PackageInfo
            private val applicationInfo: ApplicationInfo
            init{
                val context = App().getContext()
                packageManager = context.packageManager
                packageInfo = if (Build.VERSION.SDK_INT >= 33) {
                    packageManager.getPackageInfo(context.packageName, PackageManager.PackageInfoFlags.of(0L))
                }else{
                    packageManager.getPackageInfo(context.packageName, 0)
                }
                applicationInfo = if (Build.VERSION.SDK_INT >= 33) {
                    packageManager.getApplicationInfo(context.packageName, PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong()))
                }else{
                    packageManager.getApplicationInfo(context.packageName, PackageManager.GET_META_DATA)
                }
            }
            // 应用名称
            fun appName() = App().getContext().resources.getString(R.string.app_name)
            // 应用包名
            fun packageName(): String = packageInfo.packageName
            // 渠道
            fun channel() = applicationInfo.metaData?.getString("APK_CHANNEL")?:"NULL"
            // 手机厂商
            fun brand(): String = Build.BRAND
            // 手机型号
            fun model(): String = Build.MODEL
            // Android系统版本
            fun release(): String =  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) applicationInfo.compileSdkVersionCodename?:Build.VERSION.RELEASE else Build.VERSION.RELEASE
            // VersionName
            fun versionName(): String = packageInfo.versionName
            // VersionCode
            fun versionCode() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) packageInfo.longVersionCode.toString() else packageInfo.versionCode.toString()


        }

    }
}